package com.example.ymm.modules1.service.impl.hl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknForecastCache;
import com.example.ymm.modules1.entity.hl.dkn.DknForecastProduct;
import com.example.ymm.modules1.mapper.hl.DknForecastCacheMapper;
import com.example.ymm.modules1.query.hl.DklForecastQueryCriteria;
import com.example.ymm.modules1.service.dkn.DknForecastService;
import com.example.ymm.modules1.service.hl.DknForecastCacheService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.RedisUtil;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@DSTransactional
@Slf4j
@DS("slave2")
public class DknForecastCacheServiceImpl extends BaseServiceImpl<DknForecastCacheMapper, DknForecastCache> implements DknForecastCacheService {

    @Autowired
    DknForecastService dknForecastService;
    @Autowired
    RedisUtil redisUtil;


    @Override
    public void autoForecastCache2() {
        DklForecastQueryCriteria criteria=new DklForecastQueryCriteria();
        criteria.setDataTypeCode(2);
        YmPageDto ymPage=new YmPageDto();
        ymPage.setPage(1);
        ymPage.setPageSize(100);
        JSONObject jsonObject = dknForecastService.forecastTableList(criteria, ymPage);
        DknForecastCache dknForecastCache = get2Data();
        dknForecastCache.setTxtJson(jsonObject.toJSONString());
        JSONObject data = jsonObject.getJSONObject("data");
        JSONObject additionalPropeties=data.getJSONObject("additionalPropeties");
        String snapshotWeek=additionalPropeties.getString("snapshotWeek");

        String result = snapshotWeek.substring(0, 4) + "-W" + snapshotWeek.substring(4);

        dknForecastCache.setThizWeek(result);
        saveOrUpdate(dknForecastCache);
        Thread thread=new Thread(()->{
            JSONArray jsonArray = data.getJSONArray("content");
            String cache_key="cache_order_"+criteria.getDataTypeCode();
            redisUtil.remove(cache_key);
            DklForecastQueryCriteria c3=new DklForecastQueryCriteria();
            c3.setDataTypeCode(2);
            dknForecastService.forecastPlanSet(jsonArray,c3,result);
        });
        thread.start();


    }

    @Override
    public DknForecastCache get1Data() {

        return null;
    }

    @Override
    public DknForecastCache get2Data() {
        LambdaQueryWrapper<DknForecastCache> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DknForecastCache::getDataTypeCode, 2);
        List<DknForecastCache> dknForecastCaches = baseMapper.selectList(queryWrapper);
        if(dknForecastCaches.size()>0){
            return dknForecastCaches.get(0);
        }
        DknForecastCache cache=new DknForecastCache();
        cache.setDataTypeCode(2);
        cache.setId("2");
        return cache;
    }

    public JSONObject forecastDataList(DklForecastQueryCriteria criteria){
        JSONObject jsonObject =null;
        if(UtilValidate.isNotEmpty(criteria.getDataTypeCode())){
            if(criteria.getDataTypeCode().equals(2)){
                DknForecastCache forecastCache = get2Data();
                if(UtilValidate.isEmpty(forecastCache.getTxtJson())){
                    throw new YmException("请先缓存数据");
                }
                String txtJson = forecastCache.getTxtJson();
                jsonObject = JSONObject.parseObject(txtJson);
                JSONObject data = jsonObject.getJSONObject("data");

                JSONArray jsonArray = data.getJSONArray("content");
                JSONArray list = dknForecastService.forecastPlanSet(jsonArray, criteria,forecastCache.getThizWeek());
                data.put("content",list);
            }else{
                DknForecastCache forecastCache = get1Data();
                if(UtilValidate.isEmpty(forecastCache.getTxtJson())){
                    throw new YmException("请先缓存数据");
                }
                String txtJson = forecastCache.getTxtJson();
                jsonObject = JSONObject.parseObject(txtJson);
                JSONObject data = jsonObject.getJSONObject("data");

                JSONArray jsonArray = data.getJSONArray("content");
                JSONArray list = dknForecastService.forecastPlanSet(jsonArray,criteria,forecastCache.getThizWeek());
                data.put("content",list);
            }
        }
        return  jsonObject;
    }


}
